<?php
/**
 * Created by PhpStorm.
 * User: Digvijay
 * Date: 2/11/15
 * Time: 9:25 PM
 */

class Cron extends MY_Home {
    public function cancelorder($key = null){
        if($key == "CRADAYXWE"){
            $orderEnable = $this->getSystemConfig(Systemconfig::CRON_ORDER_CANCEL_ENABLE);
            if($orderEnable->getValue() == "true"){
                $orderHoursConfig = $this->getSystemConfig(Systemconfig::CRON_ORDER_CANCEL_HOUR);
                $hoursDifference = intval($orderHoursConfig->getValue());
                $notification = $this->findById("Notification", 1);
                $orders = $this->findAllPendingOrders();
                $branchEmail = "";
                foreach($orders as $order){
                    $now = new DateTime();
                    if($now->diff($order->getOrderDate())->h > $hoursDifference){
                        foreach ($order->getOrderItems() as $itm) {
                            $product = $itm->getProduct();
                            $product->addProductStockByBranch($itm->getBranch(), $itm->getQuantity());
                            $this->save($product);

                            $branchEmail = $itm->getBranch()->getEmail();
                        }
                        $order->setStatus(Order::ORDER_CANCELLED);
                        $this->save($order);

                        $agent = $order->getAgency();
                        $emailData = array("client"=>$agent, "order"=>$order);
                        $this->sendOrderCancelNotificationMail($agent->getEmail(),"Order Canceled :".$order->getId(), $emailData);
                        $this->sendOrderCancelNotificationMail($branchEmail,"Order Canceled ".$order->getId(), $emailData);
                        $this->sendOrderCancelNotificationMail($notification->getOrderPlaceEmail(),"Order Canceled ".$order->getId(), $emailData);
                    }
                }
            }
        }
    }

    public function inventoryreport($key = null){
        error_reporting(E_ERROR | E_WARNING | E_PARSE);
        if($key == "QRGASDS"){
            $quantiy =  $this->getSystemConfig(Systemconfig::CRON_INVENTORY_REPORT_MIN_QUANTITY);
            if($quantiy == null || $quantiy <= 0){
                $quantiy = 500;
            }
            $report = $this->getInventoryReport($quantiy);
            $HTMLReport = "<table style='width: 100%;border: 1px solid #CCC;'>";
            $HTMLReport .= "<thead style='font-weight: bold;background-color: #CCC;text-align: center;'><tr><td>Product Code </td><td> Branch </td><td> Available Quantity </td></tr></thead>";
            $HTMLReport .= "<tbody>";
            $count = 1;
            foreach($report as $product){
                if($count % 2 == 0){
                    $HTMLReport .= "<tr>";
                }else{
                    $HTMLReport .= "<tr style='background-color: #F2F2F2;'>";
                }

                $HTMLReport .= "<td>".$product['productName']."</td>";
                $HTMLReport .= "<td>".$product['branchName']."</td>";
                $HTMLReport .= "<td style='width:20%'>".$product['currentStock']."</td>";
                $HTMLReport .= "</tr>";

                $count++;
            }
            $HTMLReport .= "</tbody></table>";
            $notification = $this->findById("Notification", 1);
            $this->sendMail($notification->getInventoryReportEmail(),"Inventory Alert", $HTMLReport);
        }
    }
}